package com.joesem.sale.client.getui

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BitmapFactory.decodeResource
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.NotificationUtils
import com.google.gson.Gson
import com.igexin.sdk.GTIntentService
import com.igexin.sdk.message.GTCmdMessage
import com.igexin.sdk.message.GTNotificationMessage
import com.igexin.sdk.message.GTTransmitMessage
import com.jiaxing.sale.client.R
import com.joesem.sale.client.base.request
import com.joesem.sale.client.base.toast
import com.joesem.sale.client.db.LoginTable
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.internal.notify


class MsgIntentService : GTIntentService() {
    override fun onReceiveServicePid(context: Context, pid: Int) {}


    @SuppressLint("LaunchActivityFromNotification", "UnspecifiedImmutableFlag")
    override fun onReceiveMessageData(context: Context, msg: GTTransmitMessage) {
        val payload = msg.payload
        val data = String(payload)
        Log.d("PUSH_LOG", "receiver payload = $data")
        val taskid = msg.taskId
        val messageid = msg.messageId
        try {
            val gson = Gson()
            val noticeMode = gson.fromJson(data, NoticeMode::class.java)
            NotificationUtils.notify(noticeMode.data?.notify_id ?: 0) {
                it.setContentTitle(noticeMode.title)
                it.setContentText(noticeMode.content)
                it.setContentIntent(
                    PendingIntent.getBroadcast(
                        this,
                        0,
                        Intent(this, NoticeReceiver::class.java).apply {
                            putExtra("noticeData", noticeMode.data)
                        },
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                            PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
                        } else {
                            PendingIntent.FLAG_UPDATE_CURRENT
                        }
                    )
                )
                it.setAutoCancel(true)
                it.setSmallIcon(R.drawable.push_small)
                it.setLargeIcon(decodeResource(resources, R.drawable.push))
            }
        } catch (e: Exception) {
            Log.i("PUSH_LOG", "onReceiveMessageData推送消息解析失败: $e")
            toast("推送消息解析失败：$e")
        }
    }


    override fun onReceiveClientId(context: Context, clientid: String) {
        Log.e("PUSH_LOG", "onReceiveClientId -> clientid = $clientid")
        if (LoginTable.table.isLogin())
            GlobalScope.launch {
                request(request = {
                    userGTCid(hashMapOf("device_id" to clientid, "platform" to "android"))
                }, response = {

                }, responseFailed = {
                    Log.i("PUSH_LOG", "onReceiveClientId cId 上报异常Err: $it")
                }, onLoadingIn = {

                }, onLoadingQuit = {

                })
            }

    }


    override fun onReceiveOnlineState(context: Context, online: Boolean) {}


    override fun onReceiveCommandResult(context: Context, cmdMessage: GTCmdMessage) {}


    override fun onNotificationMessageArrived(context: Context, msg: GTNotificationMessage) {}


    override fun onNotificationMessageClicked(context: Context, msg: GTNotificationMessage) {}
}